<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/10/19 11:01
 * Modified Time: 2019/10/19 11:01
 * Version: 2.0
 */

namespace App\Modules\Cashier\Controllers;

use App\Exceptions\AllValidatorException;
use App\Lib\Facades\FLAuth;
use App\Modules\Cashier\Services\CashierService;
use App\Modules\Cashier\Transformers\GetBillOrderDetailTransformer;
use App\Modules\Cashier\Transformers\GetStoreBillTransformer;
use App\Modules\Cashier\Validators\CashierControllerValidator;
use App\Modules\Employee\Services\EmployeeService;
use App\Modules\Inventory\Services\InstockService;
use App\Modules\MemberCard\Services\MemberCardService;
use App\Modules\Order\Services\OrderService;
use App\Modules\Pay\Services\PayService;
use Flower\Core\Http\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CashierController extends BaseController
{
    protected $_cashierService;
    protected $_memberCardService;
	protected $employeeService;
	protected $payService;
	protected $instockService;
	protected $orderService;
    public function __construct(CashierService $cashierService,
        MemberCardService $memberCardService,EmployeeService $employeeService,PayService $payService,InstockService $instockService,OrderService $orderService) {
        $this->cashierService = $cashierService;
        $this->memberCardService = $memberCardService;
        $this->employeeService = $employeeService;
        $this->payService = $payService;
        $this->instockService = $instockService;
        $this->orderService = $orderService;
    }

    public function getRules()
    {
        return CashierControllerValidator::validator();
    }

	/**
	 * 获取店铺收银账单
	 * @param Request $request
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
    public function getStoreBill(Request $request)
    {
        $store_id = $request->input('store_id');
        $pageSize = $request->input('pageSize',10);
        $category = $request->input('category');
        $status = $request->input('status');
        $userId = $request->input('barber_id',FLAuth::id());
        $keyWord = $request->input('keyword');
        $data = $this->cashierService->getStoreBill($store_id,$category,$pageSize,$status,$userId,$keyWord);
	    $result = GetStoreBillTransformer::transform($data,$this->header['is_pad']);
        return $this->display($result);
    }

	/**
	 * 获取订单详情
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\NotFoundException
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
//    public function getBillOrderDetail(Request $request){
//    	$bill_id = $request->input('bill_id');
//    	$detail = $this->cashierService->getBillOrderDetailInfo($bill_id);
//    	$result =GetBillOrderDetailTransformer::transform($detail,$this->header['is_pad']);
//    	return $this->display($result);
//    }



	/**
	 * 服务开单
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\DbException
	 * @throws \App\Exceptions\NotFoundException
	 * bill_info = [{"assistant_id":1,"main_id":1,"is_appoint":1,"sex":1,"service_id":1,"count":1,"price":100},{"assistant_id":1,"main_id":1,"is_appoint":1,"sex":1,"service_id":1,"count":1,"price":100}]
	 */
    public function createServiceBill(Request $request)
    {
        $store_id = $request->input('store_id');
	    $appointment_id = $request->input('appointment_id',0); // 默认散客 没有预约
        $member_card_id = $request->input('member_card_id', 0);
		$params = $request->input('bill_info');
		$serviceList = json_decode($params,true);
        $user_id = FLAuth::id(); // 开单人
	    // 检测服务理发师和店铺的关联是否正确
	    foreach ($serviceList as $v){
	    	$this->employeeService->checkBarberHasSkill($v['service_id'],$v['barber_id'],$store_id);
	    }
        $billId = $this->cashierService->createServiceBill($store_id,$user_id,$appointment_id,$serviceList,$member_card_id);
        return $this->display($billId);
    }


	/**
	 * 散客服务二合一
	 * @param Request $request
	 * @return mixed
	 * @throws AllValidatorException
	 * @throws \App\Exceptions\DbException
	 * @throws \App\Exceptions\NotFoundException
	 */
    public function createAndPayServiceBill(Request $request){
	    $store_id = $request->input('store_id');
	    $appointment_id = $request->input('appointment_id',0); // 默认散客 没有预约
	    $member_card_id = $request->input('member_card_id', 0);
	    $params = $request->input('bill_info');
	    $payment = $request->input('payment');
	    $changeReason = $request->input('change_reason');
	    $finishRemark = $request->input('finish_remark');
	    $serviceList = json_decode($params,true);
	    $coupon_code = $request->input('coupon_code');
	    $user_id = FLAuth::id(); // 开单人
	    // 检测服务理发师和店铺的关联是否正确
	    foreach ($serviceList as $v){
		    $this->employeeService->checkBarberHasSkill($v['service_id'],$v['barber_id'],$store_id);
	    }
	    $billId = $this->cashierService->createServiceBill($store_id,$user_id,$appointment_id,$serviceList,$member_card_id);
	    $result = $this->orderService->finishedOrder($billId,$request->member_card_id,$params,$store_id);
	    if(!$result['status']){
		    return $this->handleApiResult($result);
	    }
	    $orderItems = $result['data']['orderItems'];
	    $memberCard = $result['data']['memberCard'];
	    $order = $this->payService->Pay($result['data']['order'],1,$billId,compact('orderItems','payment','memberCard','changeReason','finishRemark','coupon_code'));
	    return $this->success('付款成功!');
    }




    /**
     * 创建会员卡账单
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
//    public function createMemberCardBill(Request $request)
//    {
//        $store_id = $request->input('store_id');
//        $barber_id = FLAuth::id();
//        $type_id = $request->input('type_id');
//        $name = $request->input('name');
//        $mobile = $request->input('mobile');
//        $sex = $request->input('sex');
//        $main_recommend = $request->input('main_recommend');
//        $assist_recommend = $request->input('assist_recommend');
//        $billId = $this->cashierService->createMemberCardBill($store_id, $barber_id, $type_id, $name, $mobile, $sex,$main_recommend,$assist_recommend);
//        return $this->display($billId);
//    }


	public function createMemberCardBill(Request $request)
	{
		$store_id = $request->input('store_id');
		$barber_id = FLAuth::id();
		$member_card_id = $request->input('member_card_id');
		$price = $request->input('price');
//		$name = $request->input('name');
//		$mobile = $request->input('mobile');
//		$sex = $request->input('sex');
		$main_recommend = $request->input('main_recommend');
		$assist_recommend = $request->input('assist_recommend');
		$billId = $this->cashierService->createMemberCardBill($store_id, $barber_id, $member_card_id,$price,$main_recommend,$assist_recommend);
		return $this->display($billId);
	}


	/**
	 * 创建付款会员卡开卡付款
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\AllValidatorException
	 * @throws \App\Exceptions\DbException
	 * @throws \App\Exceptions\NotFoundException
	 */
	public function createAndPayMemberCardBill(Request $request){
		$store_id = $request->input('store_id');
		$barber_id = FLAuth::id();
		$member_card_id = $request->input('member_card_id');
		$price = $request->input('price');
		$present = $request->input('present');
		$payment = $request->input('payment');
		$main_recommend = $request->input('main_recommend');
		$assist_recommend = $request->input('assist_recommend');
		DB::beginTransaction();
		$billId = $this->cashierService->createMemberCardBill($store_id, $barber_id, $member_card_id,$price,$main_recommend,$assist_recommend);
		$result = $this->memberCardService->examineMemberCard($billId, 1, $price, $payment, $present);
		$memberCard = $result['member_card'];
		unset($result['member_card']);
		$this->payService->Pay($memberCard,2,$billId,$result);
		DB::commit();
		return $this->success('操作成功');
	}

    /**
     * 创建商品账单
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function createGoodsBill(Request $request)
    {
        $store_id = $request->input('store_id');
        $barber_id = FLAuth::id();
        $goodsList = $request->input('goods');
        $goodsList = json_decode($goodsList,true);
        if(empty($goodsList)){
        	throw new AllValidatorException(['msg'=>'goods参数格式错误']);
        }
        $billId  = $this->cashierService->createGoodsBill($store_id, $barber_id, $goodsList);
        return $this->display($billId);
    }


	/**
	 * 商品开单购买二合一
	 * @param Request $request
	 * @return mixed
	 * @throws AllValidatorException
	 * @throws \App\Exceptions\DbException
	 * @throws \App\Exceptions\NotFoundException
	 */
    public function createAndPayGoodsBill(Request $request){
	    $store_id = $request->input('store_id');
	    $barber_id = FLAuth::id();
	    $goodsList = $request->input('goods');
	    $goodsList = json_decode($goodsList,true);
	    $payment = $request->input('payment');
	    $remark = $request->input('remark');
	    if(empty($goodsList)){
		    throw new AllValidatorException(['msg'=>'goods参数格式错误']);
	    }
	    $billId  = $this->cashierService->createGoodsBill($store_id, $barber_id, $goodsList);
	    $result = $this->instockService->goodsSale($billId,$barber_id, $store_id, $goodsList,$payment, $remark);
	    $order = $result['order'];
	    unset($result['order']);
	    $this->payService->Pay($order,3,$billId,$result);
	    return $this->success('操作成功');

    }



    /**
     * 创建充值会员卡账单
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function createMemberCardRechargeBill(Request $request)
    {
        $store_id = $request->input('store_id');
        $barber_id = FLAuth::id();
        $member_card_id = $request->input('member_card_id');
        $main_recommend = $request->input('main_recommend');
        $assist_recommend = $request->input('assist_recommend');
        $billId = $this->cashierService->createMemberCardRechargeBill($store_id, $barber_id, $member_card_id,$main_recommend,$assist_recommend);
        return $this->display($billId);
    }


	/**
	 * 充值二合一
	 * @param Request $request
	 * @return mixed
	 * @throws AllValidatorException
	 * @throws \App\Exceptions\DbException
	 * @throws \App\Exceptions\NotFoundException
	 * @throws \Flower\Core\Repository\Exceptions\ValidatorException
	 */
	public function createAndPayMemberCardRechargeBill(Request $request)
	{
		$store_id = $request->input('store_id');
		$barber_id = FLAuth::id();
		$member_card_id = $request->input('member_card_id');
		$main_recommend = $request->input('main_recommend');
		$assist_recommend = $request->input('assist_recommend');
		$payment = $request->input('payment');
		$present = $request->input('present');
		$real_amount = $request->input('real_amount');
		$billId = $this->cashierService->createMemberCardRechargeBill($store_id, $barber_id, $member_card_id,$main_recommend,$assist_recommend);
		$recharge_num = 1;
		$result = $this->memberCardService->rechargeMemberCard($billId, $recharge_num, $payment,$real_amount,$present);
		$memberCard = $result['member_card'];
		unset($result['member_card']);
		$this->payService->Pay($memberCard,4,$billId,$result);
		return $this->success('操作成功');

	}

//    public
}
